<template>
	<view class="box">
		<HeaderBar title="首页" :opacity="0" iconColor="#ffffff"/>
		<image :src="info" mode="widthFix" class="bg"></image>
		<!-- <view class="image">
			<image :src="ossUrl + `/static/logo.png`" mode=""></image>
		</view> -->
		<view class="btns">
			<view class="btns-cancle" @click="goBack">取消 </view>
			<view @click="open()" class="btns-submit">登录</view>
		</view>
		<view class="footer" @click="change(!checked)">
			<label class="radio">
				<radio :checked="checked" />
			</label>
			请仔细阅读<text @click.stop="toRule(17)">《用户协议》</text>，<text @click.stop="toRule(18)">《隐私政策》</text>并勾选。
		</view>
		<!-- <btnlogin :zheshow='zheshow' ref="btnlogin" @submit="submit" /> -->
	</view>
</template>

<script>
	import {
		miniLogin,
		loginDecrypt,
		InformationUserLogoImg,
		InformationUserCodeLogin,
		InformationBusinessCardList
	} from '@/common/http/api.js'
	import btnlogin from '@/components/login/butlogin.vue';
	import HeaderBar from "@/components/header/header.vue";
	export default {
		components: {
			btnlogin,
			HeaderBar
		},
		data() {
			return {
				form: {
					code: ''
				},
				zheshow: false,
				info: '',
				userId: '',
				checked: false,
				login_share_business_card_id: '',
				isBack: 2, //1 返回 2 首页
				isApi: 1
			}
		},
		onLoad(e) {
			console.log('onLoad', e)
			this.$store.commit('setIsRefresh', true)
			this.$store.commit('setIsCircleRefresh', true)
			if (e.isBack) {
				this.isBack = e.isBack
			}
			if (e.userId) {
				this.userId = e.userId
			}
			if (e.shareId) {
				this.login_share_business_card_id = e.shareId
			}
			if (e.isApi) {
				this.isApi = e.isApi
			}
			InformationUserLogoImg().then(res => {
				this.info = res.data
			})
			const app = getApp();
			// if(app.globalData?.shareId){
			// 	this.form.userId = app.globalData.shareId
			// }
			console.log('app.globalData', app.globalData)
		},
		methods: {
			goBack() {
				// uni.reLaunch({
				// 	url:'/pages/loading/loading'
				// })
				uni.navigateBack()
			},
			change(e) {
				console.log('e=>', e)
				this.checked = e
			},
			toRule(id) {
				uni.navigateTo({
					url: '/pagesA/aboutUs/aboutUs?id=' + id
				})
			},
			async getIsCard() {
				//判断是否领取名片
				uni.showLoading()
				const res = await InformationBusinessCardList()
				if(Array.isArray(res.data) && res.data.length === 0) {
					//没有名片
					uni.redirectTo({
						url:'/pagesC/index/addMyCard'
					})
					return 2
				} else {
					//this.$store.dispatch('getAddress',2)
					return 1
				}
			},
			open() {
				if (this.checked === false) {
					uni.showToast({duration:3000,
						title: "请仔细阅读《用户协议》,《隐私政策》并勾选。",
						icon: 'none'
					})
					return false;
				}
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						console.log(loginRes);
						let data = {
							code: loginRes.code
						}
						if (this.userId) {
							data['share_userId'] = this.userId
						}
						if (this.login_share_business_card_id) {
							data['login_share_business_card_id'] = this.login_share_business_card_id
						}
						uni.showLoading()
						InformationUserCodeLogin(data).then(res => {
							// uni.setStorageSync("token",res.data.token)
							// uni.reLaunch({
							// 	url:'/pages/loading/loading'
							// })

							if (res.data?.token) {

								if (this.isBack == 1) {
									uni.setStorageSync("token", res.data.token)
									
									if(this.isApi == 1) {
										uni.reLaunch({
											url:'/pages/loading/loading'
										})
									} else {
										this.getIsCard().then(res=>{
											if(res == 1) {
												uni.navigateBack()
											}
										})
										
									}
								} else {
									uni.setStorageSync("token", res.data.token)
									this.$store.commit('setIsRefresh', true)
									if(this.isApi == 1) {
										uni.reLaunch({
											url:'/pages/loading/loading'
										})
									} else {
										this.getIsCard().then(res=>{
											if(res == 1) {
												uni.navigateBack()
											}
										})
									}
								}
							} else {
								//this.$refs.btnlogin.show()
								uni.showToast({duration:3000,
									title:"未获取到token，请重新登录！",
									icon: 'none'
								})
							}
						}).finally(err => {
							uni.hideLoading()
						})
					}
				});

			},
			submit(e) {

				uni.login({
					provider: 'weixin',
					success: loginRes => {
						console.log(loginRes);
						this.form.code = loginRes.code
						let data = {
							code: this.form.code,
							...e,
						}
						if (this.userId) {
							data['share_userId'] = this.userId
						}
						if (this.login_share_business_card_id) {
							data['login_share_business_card_id'] = this.login_share_business_card_id
						}
						console.log('onLoa=>', data)
						uni.showLoading()
						miniLogin(data).then(res => {

							if (this.isBack == 1) {
								uni.setStorageSync("token", res.data.token)
								uni.navigateBack()
							} else {
								uni.setStorageSync("token", res.data.token)
								this.$store.commit('setIsRefresh', true)
								this.$store.commit('setIsCircleRefresh', true)
								uni.navigateBack()
							}
						})
					}
				});

			}
		}
	}
</script>

<style lang="scss" scoped>
	// page {
		
	// }
	.bg {
		width: 100vw;
		// height: 110vh;
		position: fixed;
		left: 0;
		top: 0;
		z-index: -1;
	}

	.box {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 2;
		background: #070504;
		.image {
			width: 100%;
			text-align: center;
			margin-bottom: 350rpx;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 50%;
			}
		}

		.btns {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 20rpx;
			padding-top: 237rpx;
			.btns-cancle {
				width: 300rpx;
				height: 88rpx;
				background: #FEF493;
				border-radius: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-family: Source Han Sans CN;
				font-weight: 500;
				font-size: 30rpx;
				color: #000;
				opacity: 0.7;
			}

			.btns-submit {
				width: 300rpx;
				height: 88rpx;
				background: #FEF493;
				border-radius: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-family: Source Han Sans CN;
				font-weight: 500;
				font-size: 30rpx;
				color: #000;
				margin-left: 20rpx;
				opacity: 0.7;
			}
		}

		.footer {
			width: 690rpx;
			margin: 0 auto;
			display: inline-flex;
			align-items: center;
			flex-wrap: wrap;
			font-size: 26rpx;
			margin-bottom: 24rpx;
			white-space: normal;
			color: #fff;
			.radio {
				transform: scale(0.7)
			}

			text {
				color: #b1b1b1;
			}
		}
	}
</style>